package com.itheima.redbaby.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.R;
import com.itheima.redbaby.utils.PromptManager;

public class HomeActivity extends BaseActivity {
	private final String SERVICE_NUMBER = "01088499999";
	private ViewPager viewpager;
	private LinearLayout imgPoint;
	private final int[] imageIds = { R.drawable.image1, R.drawable.image2,
			R.drawable.image3, R.drawable.image4, R.drawable.image5, };
	private List<ImageView> imageList;
	private int lastPointPosition = 0;

	private ListView list_home;
	private static String[] names = { "限时抢购", "促销快报", "新品上架", "热门单品", "推荐品牌" };
	private static int ids[] = { R.drawable.home_classify_01,
			R.drawable.home_classify_02, R.drawable.home_classify_03,
			R.drawable.home_classify_04, R.drawable.home_classify_05 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_activity);
		imageList = new ArrayList<ImageView>();
		et_search = (EditText) findViewById(R.id.editSearchInfo);
		viewpager = (ViewPager) findViewById(R.id.viewpager);
		imgPoint = (LinearLayout) findViewById(R.id.imgPoint);
		TextView ordertel = (TextView) findViewById(R.id.orderTelTv);
		ImageView tv_ok = (ImageView) findViewById(R.id.ok);
		// 准备数据
		for (int i = 0; i < imageIds.length; i++) {
			ImageView image = new ImageView(this);
			image.setBackgroundResource(imageIds[i]);
			imageList.add(image);

			ImageView point = new ImageView(this);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			params.leftMargin = 5;
			point.setLayoutParams(params);

			if (i == 0) {
				point.setBackgroundResource(R.drawable.slide_adv_selected);
			} else {
				point.setBackgroundResource(R.drawable.slide_adv_normal);
			}
			imgPoint.addView(point);
		}
		// imgPoint.getChildAt(0).setBackgroundResource(R.drawable.slide_adv_selected);

		MyViewpagerAdapter adapter = new MyViewpagerAdapter();
		viewpager.setAdapter(adapter);

		viewpager.setOnTouchListener(new OnTouchListener() {

			int startX = 0;
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {

				case MotionEvent.ACTION_DOWN:
					startX = (int) event.getX();
					System.out.println(startX);
					break;
					
				case MotionEvent.ACTION_UP:
					int endX = (int) event.getX();
					System.out.println(endX);
					if (Math.abs(endX - startX) > 10) {

					} else {
						Intent intent = new Intent(HomeActivity.this,
								PromBulletinActivity.class);
						startActivity(intent);

					}
					startX = endX;
					break;
				}
				return false;
			}
		});
		
		tv_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String keyword = et_search.getText().toString().trim();
				if("".equals(keyword)) {
					Toast.makeText(HomeActivity.this, "请输入查询的内容", Toast.LENGTH_SHORT).show();
					return;
				}
				
				Intent intent = new Intent(HomeActivity.this, SearchResultActivity.class);
				intent.putExtra("product", keyword);
				startActivity(intent);
			}
		});
		
		ordertel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + SERVICE_NUMBER));  
	            startActivity(intent);
			}
		});

		viewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				position = position % imageList.size();
				// 让当前位置的点为选中状态
				imgPoint.getChildAt(position).setBackgroundResource(
						R.drawable.slide_adv_selected);
				// 让上一个点显示为默认状态
				imgPoint.getChildAt(lastPointPosition).setBackgroundResource(
						R.drawable.slide_adv_normal);
				lastPointPosition = position;
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {}
			@Override
			public void onPageScrollStateChanged(int arg0) {}
		});

		int firstPostion = Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2
				% imageList.size();
		viewpager.setCurrentItem(firstPostion);
		handler.sendEmptyMessageDelayed(0, 2000);
		isRunning = true;

		list_home = (ListView) findViewById(R.id.custonInfoListView);
		// 设置适配器
		list_home.setAdapter(new HomeAdapter());
		list_home.setOnItemClickListener(new OnItemClickListener() {
  
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent;
				switch (position) {
				case 0:// 进入限时抢购专题
					intent = new Intent(HomeActivity.this,
							LimitBuyActivity.class);
					startActivity(intent);

					break;
				case 1:// 进入促销快报专题
					intent = new Intent(HomeActivity.this,
							PromBulletinActivity.class);
					startActivity(intent);

					break;
				case 2:// 进入新品上架专题
					intent = new Intent(HomeActivity.this, NewActivity.class);
					startActivity(intent);

					break;
				case 3:// 进入热门单品专题
					intent = new Intent(HomeActivity.this, HotActivity.class);
					startActivity(intent);

					break;
				case 4:// 进入推荐品牌专题
					intent = new Intent(HomeActivity.this, BrandActivity.class);
					startActivity(intent);

					break;
				default:
					break;
				}
			}
		});
	}

	private boolean isRunning = false;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			viewpager.setCurrentItem(viewpager.getCurrentItem() + 1);
			if (isRunning) {
				handler.sendEmptyMessageDelayed(0, 5000);
			}
		};
	};
	private EditText et_search;

	protected void onDestroy() {
		GloableParams.isLogin = false;
		GloableParams.userid = -1;
		GloableParams.username = "";
		super.onDestroy();
		isRunning = false;
	}
	
	class MyViewpagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return Integer.MAX_VALUE;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// 1. 获得指定位置的view 并添加到container
			View view = imageList.get(position % imageList.size());
			container.addView(view);
			// 2.返回一个和view有关系的对象
			return view;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			// view是instantiateItem方法中添加至container的view
			// object是instantiateItem方法返回的对象
			return view == object;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// super.destroyItem(container, position, object);
			container.removeView((View) object);

		}
	}
	
	private class HomeAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return names.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 把布局文件-->View
			View view = View.inflate(HomeActivity.this, R.layout.home_item,
					null);
			ImageView iv_icon = (ImageView) view.findViewById(R.id.imgIcon);
			TextView tv_name = (TextView) view.findViewById(R.id.textContent);
			tv_name.setText(names[position]);
			iv_icon.setImageResource(ids[position]);
			return view;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
	}
}
